package cn.tedu.weibo.controller;

import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo.pojo.vo.WeiboIndexVO;
import cn.tedu.weibo.response.JsonResult;
import cn.tedu.weibo.service.WeiboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/weibos")
public class WeiboController {
    @Autowired
    private WeiboService weiboService;
    @PostMapping("add-new")
    public JsonResult addNew(WeiboDTO weiboDTO){
        System.out.println("weiboDTO = " + weiboDTO);
        weiboService.addNew(weiboDTO);
        return JsonResult.ok();
    }
    @GetMapping("list")
    public JsonResult listForIndex(){
        List<WeiboIndexVO> list = weiboService.listForIndex();
        return JsonResult.ok(list);
    }
    //@GetMapping注解是基于@RequestMapping注解的.如果不写值.下方等价于:
    //http://localhost:8080/v1/weibos
    //但注意:同种请求方式只能有一个这样写,不然找不到!自己写的时候尽量不要这么用!
    @GetMapping("")
    public JsonResult listForIndexByUserId(Long userId){
        System.out.println("userId = " + userId);
        List<WeiboIndexVO> list = weiboService.listForIndexByuserId(userId);
        return JsonResult.ok(list);
    }
    @PostMapping("{id}/delete")
    public JsonResult deleteById(@PathVariable Long id){
        weiboService.deleteById(id);
        return JsonResult.ok();
    }
    @GetMapping("{id}/detail")
    public JsonResult detailById(@PathVariable Long id){
        WeiboDetailVO weiboDetailVO = weiboService.detailById(id);
        return JsonResult.ok(weiboDetailVO);

    }
}
